package com.haynes.meetxiaohai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haynes.meetxiaohai.common.R;
import com.haynes.meetxiaohai.entity.Employee;
import com.haynes.meetxiaohai.service.EmployeeService;
import com.sun.istack.internal.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Haynes
 * @Date 2023/3/16 21:33
*/
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService service;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
//        1.将密码使用md5加密
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
//        2.根据页面提交的用户名查询
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username",employee.getUsername());
        Employee emp = service.getOne(wrapper);
//        3.判断用户是否存在
        if (emp==null){
            return R.error("用户不存在！");
        }
//        4.密码比对，不一致则返回登录失败结果
        if (!emp.getPassword().equals(password)){
            return R.error("密码不正确！");
        }
//        5.该用户是否被禁用,禁用则返回登录失败结果
        if (emp.getStatus()==0){
            return R.error("用户已被禁用！");
        }
//        6.将当前ID放入session
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功~");
    }

    /**
     * 添加员工
     * @param employee 员工对象
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("当前传入的值为："+employee.toString());
//        1.设置初始密码123456，但需要md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        4.保存到数据库中
        service.save(employee);
        return R.success("添加成功！");
    }

    /**
     * 分页查询数据
     * @param page 页码
     * @param pageSize 条数
     * @param name 用户名
     * @return 员工数据集合
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize, String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
//        1.创建分页插件对象
        Page<Employee> pageInfo = new Page<Employee>(page,pageSize);
//        2.创建条件过滤器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
//        2.1模糊查询
        queryWrapper.like(name!=null,Employee::getName,name);
//        2.2根据更新日期排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
//        3.执行查询
        service.page(pageInfo,queryWrapper);
        return  R.success(pageInfo);
    }

    /**
     * 根据ID更新员工信息
     * @param employee 员工对象
     * @return R<String>
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@NotNull @RequestBody Employee employee){
        log.info(employee.toString());
//        1.更新数据
        service.updateById(employee);
        return R.success("更新成功！");

    }

    /**
     * 根据ID查询员工信息
     * PathVariable：取路径变量中的值
     * @param id 员工ID
     * @return 员工对象
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") Long id){
        log.info("根据ID查询员工信息~~");
        Employee employee = service.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("没有查询到对应的员工");
    }

}
